#include "Deck.h"
#include <stdlib.h>
#include <time.h>

Deck::Deck()
{
    static bool first = true;
    if( first )
    {
        srand(time(NULL));
        first = false;
    }
}

Deck::~Deck()
{
    for( int i = 0; i<getSize(); ++i)
    {
        delete _deck[i];
    }
    _deck.clear();
}

void Deck::insert( Card* card )
{
	_deck.push_back( card );
}

Card* Deck::remove()
{
	Card* card = _deck[ (int)_deck.size() - 1 ];
	_deck.pop_back();
	return card;
}

void Deck::shuffle()
{
    int size = getSize();
    for( int i = 0; i < size; ++i )
    {
        swap(i, randLim(size-1));
    }
}

void Deck::fill()
{
    Card::Suit suit;
    for( int type = 0; type < 3; ++type )
    {
        switch (type)
        {
            case 0:
                suit = Card::CLUBS;
                break;
            case 1:
                suit = Card::DIAMONDS;
                break;
            case 2:
                suit = Card::HEARTS;
                break;
            case 3:
                suit = Card::SWORDS;
                break;
        }
        for( int val = 1; val <= 13; ++val )
        {
            Card* card = new Card(val, suit);
            _deck.push_back(card);
        }
    }
}

int Deck::getSize()
{
    return (int)_deck.size();
}

void Deck::swap(int pos1, int pos2)
{
    Card* aux = _deck[pos1];
    _deck[pos1] = _deck[pos2];
    _deck[pos2] = aux;
}

int Deck::randLim(int limit)
{
    float fRand = (float)rand()/RAND_MAX;
    fRand *= limit;
    return (int)fRand;
}